﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DalatCooking.Core;
public partial class Role_RoleManager : System.Web.UI.Page
{
    private void Page_Load(object sender, System.EventArgs e)
    {

        if (Context.User.Identity.IsAuthenticated)
        {
            if (!(Context.User is CustomPrincipal))
            {
                // ASP.NET's regular forms authentication picked up our cookie, but we
                // haven't replaced the default context user with our own. Let's do that
                // now. We know that the previous context.user.identity.name is the e-mail
                // address (because we forced it to be as such in the login.aspx page)				
                CustomPrincipal newUser = new CustomPrincipal(Context.User.Identity.Name);
                Context.User = newUser;
            }
        }

        if (!Context.User.Identity.IsAuthenticated)
            Response.Redirect("Login.aspx");

        if (!((CustomPrincipal)Context.User).IsInRole("SuperUser"))
            Response.Redirect("Login.aspx");

        if (!Page.IsPostBack)
            BindGrid();
    }

    private void BindGrid()
    {
       RolesGrid.DataSource = RoleManager.FindAll();
        RolesGrid.DataBind();
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: This call is required by the ASP.NET Web Form Designer.
        //
        base.OnInit(e);
        InitializeComponent();
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.Delete.Click += new System.EventHandler(this.Delete_Click);
        this.AddRole.Click += new System.EventHandler(this.AddRole_Click);
        this.RolesGrid.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.RolesGrid_CancelCommand);
        this.RolesGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.RolesGrid_EditCommand);
        this.RolesGrid.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.RolesGrid_UpdateCommand);
        this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void AddRole_Click(object sender, System.EventArgs e)
    {
        if (Page.IsValid)
        {
            Role role = new Role();
            role.RoleName = RoleName.Text;
            RoleManager.Insert(role);

            RoleName.Text = "";
            BindGrid();
        }
    }

    private void RolesGrid_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        RolesGrid.EditItemIndex = e.Item.ItemIndex;
        BindGrid();
    }

    private void RolesGrid_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        RolesGrid.EditItemIndex = -1;
        BindGrid();
    }

    private void RolesGrid_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
        /*
        Role role = new RoleManager. (int.Parse(((Label)e.Item.Cells[3].FindControl("RoleID")).Text));
        role.RoleName = ((TextBox)e.Item.Cells[3].FindControl("RoleNameEdit")).Text;
        role.Update();

        RolesGrid.EditItemIndex = -1;
        BindGrid();
         * */
    }

    private void Delete_Click(object sender, System.EventArgs e)
    {
        /*
        Role role = new Role(int.Parse(RoleIDHidden.Value));
        role.Delete();

        BindGrid();
         * */
    }
}